package com.chao.wind3j.controller;

import com.chao.wind3j.base.apiResult.ApiResult;
import com.chao.wind3j.base.config.commonConfig.pageConfig.NeedCommonPage;
import com.chao.wind3j.base.config.commonConfig.requestLogConfig.requestWrapper.CustomRequestWrapper;
import com.chao.wind3j.base.config.dozerConfig.CastBean;
import com.chao.wind3j.base.parentClass.controller.BaseController;
import com.chao.wind3j.model.base.pageVo.WPage;
import com.chao.wind3j.model.dataBaseModel.Wind3User;
import com.chao.wind3j.model.dataVo.LoginWind3UserVo;
import com.chao.wind3j.model.dataVo.Wind3UserVo;
import com.chao.wind3j.model.searchParams.Wind3UserParams.*;
import com.chao.wind3j.service.Wind3UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * Wind3 用户管理类
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

  @Autowired
  private Wind3UserService wind3UserService;

  @Autowired
  private CastBean castBean;

  /**
   * 获取Wind3用户列表
   * @param wind3UserRequest 查询用wind3用户类
   * @return 用户列表
   */
  @GetMapping("/getList")
  @NeedCommonPage
  public ApiResult<List<Wind3UserVo>> getWind3UserList(Wind3UserSearchRequest wind3UserRequest, WPage page) {
    var wind3User = this.castBean.castModelToVo(wind3UserRequest, Wind3User.class);
    var wind3UserList = this.wind3UserService.getWind3UserList(wind3User);
    var list = this.castBean.castModelToVo(wind3UserList, Wind3UserVo.class);
    return ApiResult.success(list);
  }

  /**
   * 根据id获取单个用户
   * @param userId 用户的id
   * @return 单个的用户
   */
  @GetMapping("/getSingleUser")
  public ApiResult<Wind3UserVo> getWind3UserById(String userId) {
    var wind3User = this.wind3UserService.getOneWind3UserById(userId);
    return ApiResult.success(this.castBean.castModelToVo(wind3User, Wind3UserVo.class));
  }

  /**
   * 新增一个Wind3用户
   * @param wind3UserAddRequest 新增用win3用户类
   * @return 已成功插入用户
   */
  @PostMapping("/addUser")
  public ApiResult<Boolean> addWind3User(@RequestBody Wind3UserAddRequest wind3UserAddRequest){
    var wind3User = this.castBean.castModelToVo(wind3UserAddRequest, Wind3User.class);
    return ApiResult.success(true);
  }

  /**
   * 物理删除或逻辑删除用户
   * @param wind3UserDelRequest 删除用用户类
   * @return 已成功执行删除动作
   */
  @PostMapping("/deleteUser")
  public ApiResult<Boolean> deleteWind3User(@RequestBody Wind3UserDelRequest wind3UserDelRequest) {
    if (wind3UserDelRequest.getIsPhyDel()) {
      this.wind3UserService.deleteWind3User(wind3UserDelRequest.getUserId());
    } else {
      this.wind3UserService.tombstoneWind3User(wind3UserDelRequest.getStatus(), wind3UserDelRequest.getUserId());
    }
    return ApiResult.success(true);
  }

  /**
   * 根据id更新wind3User用户
   * @param wind3UserUpdateRequest 更新用户类
   * @return 已成功执行更新用户
   */
  @PostMapping("/updateUser")
  public ApiResult<Boolean> updateWind3User(@RequestBody Wind3UserUpdateRequest wind3UserUpdateRequest) {
    var wind3User = this.castBean.castModelToVo(wind3UserUpdateRequest, Wind3User.class);
    this.wind3UserService.updateWind3User(wind3User);
    return ApiResult.success(true);
  }

  /**
   * 用户登录功能
   * @param customRequestWrapper request对象
   * @param loginParam 用户登录对象
   * @return 返回token
   * @throws Exception 抛出的异常
   */
  @PostMapping("/login")
  public ApiResult<LoginWind3UserVo> loginSystem(@ApiIgnore CustomRequestWrapper customRequestWrapper, @RequestBody Wind3UserLoginParam loginParam) throws Exception {
    var loginUser = this.wind3UserService.login(customRequestWrapper, loginParam);
    return ApiResult.success(loginUser);
  }

  /**
   * 根据token获取登录系统的用户信息
   * @param customRequestWrapper request对象
   * @return 登录系统的用户对象
   */
  @GetMapping("/getUserInfoByToken")
  public ApiResult<LoginWind3UserVo> getWind3UserInfoByToken(@ApiIgnore CustomRequestWrapper customRequestWrapper) {
    var loginUserWind3User = this.wind3UserService.getUserInfoByToken(customRequestWrapper.getRequestInfo().getAuthToken().getToken());
    var loginWind3UserVo = new LoginWind3UserVo();
    loginWind3UserVo.setWind3User(this.castBean.castModelToVo(loginUserWind3User, Wind3UserVo.class));
    loginWind3UserVo.setToken(customRequestWrapper.getRequestInfo().getAuthToken().getToken());
    return ApiResult.success(loginWind3UserVo);
  }

  /**
   * 退出系统
   * @param customRequestWrapper request对象
   * @return 已成功退出系统
   */
  @GetMapping("/logout")
  public ApiResult<Boolean> logoutSystem(@ApiIgnore CustomRequestWrapper customRequestWrapper) {
    this.wind3UserService.logout(customRequestWrapper);
    return ApiResult.success(true);
  }
}
